﻿using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;

namespace SIMcSDK
{
    public delegate void OnBuddyStatusChanged(int buddyId, int status, StringBuilder statusText);
    public delegate void OnWatchInfoReceived(StringBuilder statusText);

    public class PresenceControl
    {
        #region DLL declarations
        [DllImport("pjsipDll.dll")]
        private static extern int dll_addBuddy(string uri, bool subscribe);
        [DllImport("pjsipDll.dll")]
        private static extern int dll_removeBuddy(int buddyId);
        [DllImport("pjsipDll.dll")]
        private static extern int dll_setStatus(int accId, int presence_state);
        [DllImport("pjsipDll.dll")]
        private static extern int getWinfo(string dest_uri);

        #endregion DLL declarations

        #region Callback declarations
        // Callback function registration declarations 
        // passing delegate to unmanaged code (.dll)
        [DllImport("pjsipDll.dll")]
        private static extern int onBuddyStatusChangedCallback(OnBuddyStatusChanged cb);
        
        // Static declaration because of CallbackonCollectedDelegate exception!
        static OnBuddyStatusChanged bscDel = new OnBuddyStatusChanged(onBuddyStatusChanged);
               
        public static event OnBuddyStatusChanged buddyStatusChanged;

        private static void onBuddyStatusChanged(int buddyId, int status, StringBuilder statusText)
        {
            if (buddyStatusChanged != null)
                buddyStatusChanged(buddyId, status, statusText);
            return;
        }

        // Callback function registration declarations 
        // passing delegate to unmanaged code (.dll)
        [DllImport("pjsipDll.dll")]
        private static extern int onWatchInfoReceived(OnWatchInfoReceived cb);

        // Static declaration because of CallbackonCollectedDelegate exception!
        static OnWatchInfoReceived wirDel = new OnWatchInfoReceived(onWatcherInfo);

        public static event OnWatchInfoReceived watcherInfoReceived;

        private static void onWatcherInfo(StringBuilder info)
        {
            if (watcherInfoReceived != null)
                watcherInfoReceived(info);
            return;
        }
        #endregion

        #region Methods        

        /// <summary>
        /// Static initializer. Call this method to set callbacks from SIP stack. 
        /// </summary>
        public static void initialize()
        {
            // assign callbacks
            onBuddyStatusChangedCallback(bscDel);
            onWatchInfoReceived(wirDel);
        }


        // Buddy list handling
        public static int addBuddy(string ident)
        {
            return dll_addBuddy(ident, true);
        }

        public static int delBuddy(int buddyId)
        {
            return dll_removeBuddy(buddyId);
        }

        public static int getWatcherInfo(string uri)
        {
            return getWinfo(uri);
        }

        public static int setStatus(int accId, int status)
        {
            return dll_setStatus(accId, status);
        }

        #endregion Methods
    }
}
